//
//  NoteInfo+YL8G.m
//  YL8G
//
//  Created by Liccon Chang on 12-8-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NoteInfo+YL8G.h"
#import "NSDictionary+Custom.h"
#import "NSDate+Custom.h"
#import "Instance.h"

@implementation NoteInfo(YL8G)

+(BOOL)createMarkTableWithDatabase:(FMDatabase*)aDatabase
{
    NSString* sqlCmd=[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS marknote ("
                      "id INTEGER PRIMARY KEY AUTOINCREMENT," 
                      "noteId INTEGER,"
                      "categoryId nvarchar(50),"
                      "authorId INTEGER,"
                      "createTime DOUBLE,"
                      "timesStamp DOUBLE,"
                      "pageNo INTEGER,"
                      "offset INTEGER,"
                      "title nvarchar(50),"
                      "authorName nvarchar(50))"];
    return [aDatabase executeUpdate:sqlCmd];
}

+(BOOL)deleteAllMarks
{
    return [[Instance instance].dataBase executeUpdate:@"delete from marknote"];
}

+(NSArray*)queryAllMarks
{
    NSString* sqlCmd=@"SELECT * from marknote";
    FMResultSet* resultSet=[[Instance instance].dataBase executeQuery:sqlCmd];
    NSMutableArray* array=[[NSMutableArray alloc] initWithCapacity:10];
	while ([resultSet next])
	{  
		NoteInfo* msg=[[NoteInfo alloc] initWithResultSet:resultSet]; 
		[array addObject:msg];
		[msg release];
	} 
	return [array autorelease];
}

- (BOOL)deleteMarkRecordFromDatabase
{
    return [[Instance instance].dataBase executeUpdate:@"DELETE FROM marknote WHERE noteId=? AND categoryId=?",
            [NSNumber numberWithInt:self.idd],self.categoryId];
}

- (BOOL)insertMarkRecordIntoDatabase
{
	BOOL ret=NO;
    NSDate* date=[NSDate dateWithDateString:self.createTime];
    double timeStamp=[date timeIntervalSince1970];
    
	ret=[[Instance instance].dataBase executeUpdate:@"INSERT INTO marknote (noteId,categoryId,authorId,createTime,timesStamp,pageNo,offset,title,authorName) "
		 "VALUES (?,?,?,?,?,?,?,?,?)",
		 [NSNumber numberWithInt:self.idd],
		 self.categoryId,
		 [NSNumber numberWithInt:self.authorId],
		 self.createTime,
		 [NSNumber numberWithDouble:timeStamp],
		 [NSNumber numberWithInt:self.pageNo],
         [NSNumber numberWithInt:self.offset],
		 self.title,
         self.author];
	return ret;
}

- (BOOL)updateSetMarkRecordInDatabase
{
    BOOL ret=NO;
    NSDate* date=[NSDate dateWithDateString:self.createTime];
    double timeStamp=[date timeIntervalSince1970];
    
	ret=[[Instance instance].dataBase executeUpdate:@"UPDATE marknote SET  timesStamp=? , pageNo=? , offset=? WHERE noteId=? AND categoryId=?",
		 [NSNumber numberWithDouble:timeStamp],
         [NSNumber numberWithInt:self.pageNo],
         [NSNumber numberWithInt:self.offset],
		 [NSNumber numberWithInt:self.idd],
         self.categoryId];
	return ret;
}

- (BOOL)addToMarkRecord
{
    return [self isExistInMarks]>=0?[self updateSetMarkRecordInDatabase]:
    [self insertMarkRecordIntoDatabase];
    
}

- (int)isExistInMarks
{
    
    FMResultSet* resultSet=[[Instance instance].dataBase executeQuery:@"SELECT * FROM marknote where noteId=? AND categoryId=?",[NSNumber numberWithInt:self.idd],self.categoryId];
    int retval=-1;
    if ([resultSet next]) {
        retval=[resultSet intForColumn:@"noteId"];
    }
    
    return retval;
}

@end
